// pages/classRoom/classRoom.js
const app = getApp()
const env = app.globalData.env
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    iconList: [{
        name: "签到",
        url: "../../icon/classRoom/签到.png",
      },
      {
        name: "学习",
        url: "../../icon/classRoom/工作任务.png",
      },
      {
        name: "导出",
        url: "../../icon/classRoom/修改成绩.png",
      }
    ],
    // 控制单一切换
    choose: false,
    cardList: [{
      name: "公告"
    }, {
      name: "实验"
    }, {
      name: "成绩"
    }, {
      name: "作业"
    }, {
      name: "话题"
    }],
    // 0--老师 1--学生
    buttonFlag:-1

  },
  export(e){
    let _this=this
    let person
    wx.getStorage({
      key: 'user',
      success:res=>{
        console.log(res,e)
        if(e.currentTarget.dataset.field==2){
          wx.request({
            url: 
            env != 1?'http://localhost:88/api/exportAll':'https://www.wantstong.com/api/exportAll',
            method:'POST',
            data:{
              emitTopData:_this.data.emitTopData,
              person:res.data
            },
            success:res=>{
              wx.showLoading({
                title: '导出中...',
              })
              let excelTime=setInterval(function(){
                wx.downloadFile({
                  url: res.data.http,
                  success: res => {
                    const tempFilePath = res.tempFilePath
                    wx.saveFile({
                      tempFilePath,
                      success: res => {
                        const saveFilePath = res.savedFilePath;
                        wx.openDocument({
                          filePath: saveFilePath,
                          success: res => {
                            wx.hideLoading({
                              success: (res) => {},
                            })
                            clearInterval(excelTime)
                            console.log("打开文档成功")
                          }
                        })
                      }
                    })
                  }
                })
              },2000)
          

            }
          })
          console.log("emitp",this.data.emitTopData)
        }
       
      }
    })
  
  },
  isActive(e) {
    let currentIndex = e.currentTarget.dataset.index
    let oldIndex = this.data.currentIndex
    if (currentIndex == oldIndex != 0) {
      console.log("相等")
    } else {
      this.setData({
        currentIndex: currentIndex,
        choose: true
      })
    }
  },
  gotoMember() {
    let _this = this
    wx.navigateTo({
      url: '/pages/classMember/classMember',
      success: function (res) {
        res.eventChannel.emit('gotoClassMember', {
          data: _this.data.emitTopData
        })
      }
    })

  },
  searchTask() {
    console.log("searchTask")
    console.log(this.data.emitTopData)
    let _this = this

    wx.request({
      url: env != 1 ? 'http://localhost:88/api/taskSearch' : 'https://www.wantstong.com/api/taskSearch',
      method: 'POST',
      data: {
        classId: _this.data.emitTopData.courseId
      },
      success(res) {
        let taskData = res.data.data
        _this.setData({
          taskData: res.data.data
        })
        console.log(res.data.data)
      },

    })

  },
  gotoAppoint(e) {
    let _this = this
    let itemData = e.currentTarget.dataset.itemdata
    wx.request({
      url: env != 1 ? 'http://localhost:88/api/taskAppointment' : 'https://www.wantstong.com/api/taskAppointment',
      method: "POST",
      data: {
        item: itemData,
        user: _this.data.user
      },
      success: (res) => {
        res.data.msg == "success" ? wx.showToast({
          title: '预约成功',
        }) : wx.showToast({
          title: '预约失败',
        })
        console.log(res.data.msg)
      }
    })
    console.log("预约按钮", e.currentTarget.dataset.itemdata)
  },
// 前往成绩自定义
  gotogradeFrame(e){
    let item=e.currentTarget.dataset.card
    wx.setStorage({
      data: item,
      key: "taskData",
    })
    wx.navigateTo({
      url: '/pages/gradeFrame/gradeFrame',
      success:(res)=>{

        res.eventChannel.emit("taskData",{
          data:item
        })
      }
    })

  },
  slideButtonTap(e) {
    let _this=this
    let taskData=e.currentTarget.dataset.task
    wx.request({
      url: 'https://www.wantstong.com/api/taskDelete',
      method:"POST",
      data:{
        courseData:_this.data.emitTopData,
        taskData:taskData
      },
      success:res=>{
        wx.showToast({
          title: '删除成功',
        })
        console.log("res",res)
        if(res.data.msg=="success"){
          _this.searchTask()
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this=this
   wx.getStorage({
     key: 'clickCourseId',
     success:res=>{
      _this.setData({
        emitTopData: res.data
      })
     }
   })
    this.setData({
      slideButtons: [{
        type: 'warn',
        text: '删除',
        extClass: 'test',
        src: '/page/weui/cell/icon_del.svg', // icon的路径
      }],
    });
    this.isTeacher()
    
    // let _this = this
    // const eventChannel = this.getOpenerEventChannel()
    // eventChannel.on('acceptClassData', function (data) {
    //   let classData = data.data
    //   _this.setData({
    //     emitTopData: classData
    //   })
    //   console.log("topData", _this.data.emitTopData)
    // })

    this.getUser()
  },

// 获取storage里面的user
  getUser() {
    let _this = this
    wx.getStorage({
      key: 'user',
      success: (res) => {
        console.log("getUser", res)
        _this.setData({
          user: res.data
        })

      }
    })
  },

  isTeacher(){
    let _this=this
    wx.getStorage({
      key: 'user',
      success:res=>{
        console.log(_this.data.emitTopData)
        let userId=res.data.userId
        wx.request({
         url: env != 1 ? 'http://localhost:88/api/isTeacher' : 'https://www.wantstong.com/api/isTeacher',
         method:'POST',
         data:{
           classId:_this.data.emitTopData.courseId,
           userId:userId
         },
         success:res=>{
           if(res.data.id=="teacher"){
             _this.setData({
               buttonFlag:0
             })
           }else{
            _this.setData({
              buttonFlag:1
            })
          }
         }
        })
        console.log("isTAeacher",res.data.userId)
      }

    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.searchTask()


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.isTeacher()

    this.searchTask()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})